package tr.edu.itu.yoklama;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tr.edu.itu.hibernate.SessionFactoryUtil;
import tr.edu.itu.hibernate.entity.Honey;

public class TestExample {

  /**
   * @param args
   */
  public static void main(String[] args) {
	  
//    Honey forestHoney = new Honey();
//    forestHoney.setId(1);
//    forestHoney.setName("32323ey");
//    forestHoney.setTaste("vdsfsdf sweet");
//    Honey countryHoney = new Honey();
//    countryHoney.setName("country honey");
//    countryHoney.setTaste("tasty");
//    createHoney(forestHoney);
//    createHoney(countryHoney);
//    // our instances have a primary key now:
//    listHoney();
//    //deleteHoney(countryHoney);
//    listHoney();
//    forestHoney.setName("Norther Forest Honey");
//    updateHoney(forestHoney);
	  
	  List<Honey> list =listHoney();
	  for(Honey h:list){
		  System.out.println(h);
	  }
    

  }

  private static List<Honey> listHoney() {
    Transaction tx = null;
    List<Honey>list = new LinkedList<Honey>();
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      List honeys = session.createQuery("select h from Honey as h")
              .list();
      for (Iterator iter = honeys.iterator(); iter.hasNext();) {
        list.add((Honey) iter.next());
      }
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
          System.out.println("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }


    }
    return list;
  }

  private static void deleteHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.delete(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
        	System.out.println("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }

  private static void createHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.save(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
        	System.out.println("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }

   private static void updateHoney(Honey honey) {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      tx = session.beginTransaction();
      session.update(honey);
      tx.commit();
    } catch (RuntimeException e) {
      if (tx != null && tx.isActive()) {
        try {
// Second try catch as the rollback could fail as well
          tx.rollback();
        } catch (HibernateException e1) {
        	System.out.println("Error rolling back transaction");
        }
// throw again the first exception
        throw e;
      }
    }
  }
}
